'''
Author: Sjy
Description: 等值面界面
'''

# -*- coding: utf-8 -*-

from PyQt5 import  QtWidgets, QtCore, QtGui

import gui.controls.globalvar as GL
import gui.controls.apis as apis
import gui.components.message_box as MBox
import gui.components.util_file as UFile
from .ui_isourfaceAverage import ui_isourfaceAverage

global CONFIG_FILE
CONFIG_FILE = 'config/isourface.json'

class DlgIsourfaceAverage(QtWidgets.QDialog):
    sigGetSeries = QtCore.pyqtSignal(list)
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent)
        self.ui = ui_isourfaceAverage()
        self.ui.setupUi(self)
        self.series = []
        self.config = UFile.open_json(CONFIG_FILE)
        

        configAverage = self.config.get('average', {})
        seriesTypes = configAverage.get('seriesTypes', [])
        sampleNum = configAverage.get('sampleNum', 10)
        precision = configAverage.get('precision', 6)
        seriesType = configAverage.get('seriesType', 'Linear')


        self.ui._spinboxPrecision.setValue(precision)
        self.ui._spinboxPrecision.setValue(precision)
        self.ui._spinboxNum.setValue(sampleNum)
        self.ui.initTypeOptions(seriesTypes)

        index = self.ui._comboxType.findData(seriesType, QtCore.Qt.UserRole) 
        if index != -1:
            self.ui._comboxType.setCurrentIndex(index)

    def get_range(self, warning=False):
        flag = GL.get_value('threshold.flag')
        name = GL.get_value('threshold.name')
        if not flag or not name:
            warning and MBox.warning('请先设置要查看阈值的类型和名称！')
            return
        # 获取阈值范围
        colorRange = apis.get_show_threshold_range(name, flag)
        if not colorRange or len(colorRange) < 4:
            warning and MBox.warning('请先设置阈值范围')
            return 
        [lower, upper, *args] = colorRange
        return  {
            'min': lower,
            'max': upper
        }
    @QtCore.pyqtSlot()
    def on_btnRefreshRange_clicked(self):
        # 点击刷新获取阈值福安为 
        range = self.get_range(warning=True)
        if range != None:
            self.ui.set(range)
        
    @QtCore.pyqtSlot()
    def on_btnGenerate_clicked(self):
        data = self.ui.get()
        if not isinstance(data, dict):
            return MBox.warning(data)
        seriesType = data.get('seriesType')
        min = data.get('min')
        max = data.get('max')
        sampleNum = data.get('sampleNum')
        precision = data.get('precision') or 6
        series = []
        if seriesType == 'Linear':
            series = self.getSeriesByLinear(min, max, sampleNum, precision)
        self.ui.setSeries(series)

        self.series = series
        
    @QtCore.pyqtSlot()
    def on_btnOK_clicked(self):   
        if not self.series:
            return MBox.warning('请先生成数列')
        self.sigGetSeries.emit(self.series)
        self.accept()
    @QtCore.pyqtSlot()
    def on_btnApply_clicked(self):   
        if not self.series:
            return MBox.warning('请先生成数列')
        self.sigGetSeries.emit(self.series)
    # 获取线型等差数列
    def getSeriesByLinear(self, min, max, num, precision):
        # 注意 最小值，最大值占用两个数字
        step = (max - min) / (num - 1)
        series = [str(min)]
        for i in range(1, num-1):
            # 保留指定精度的小数
            series.append(str(round(min + step * i, precision)))
        series.append(str(max))
        return series
    def init(self, data:dict={}):
        self.series = []
        self.ui.setSeries()
        range = self.get_range()
        if range != None:
            self.ui.set(range)
        
